﻿package org.net{ 

	import flash.events.*;
	import flash.net.*;
	import flash.utils.describeType;
	import org.abcFrame.control.*
	import org.abcFrame.enmu.*
	
	/**
	 * Red5交互类 
	 * @author Administrator
	 * 
	 */	
	public class AbcRed5{
		
		public var nc:NetConnection;
		
		private var isinit:Boolean=false
		
		private static var _instance:AbcRed5; 
		public static function get instance():AbcRed5 {
			if (_instance == null) { 
				_instance = new AbcRed5(); 
				} 
			return _instance; 
		}

		public function AbcRed5():void { } 

		/**
		 * 建立AbcRed5服务器连接 
		 * @param rtmp 协议地址
		 * @param username 登陆用户名
		 * 
		 */		
		public function connect(rtmp:String,username:String) :void
		{
			//创建新连接之前关闭之前建立的连接
			disconnect() 
			
			var obj:Object = { }
			obj.sendMessage = function(obj) {AbcRed5Data.instance.message = obj; AbcRed5Data.instance.dispatcher("message") }
            obj.onBWDone = function(){}
			
			nc = new NetConnection( );
			nc.client = obj;

            nc.connect(rtmp,username);
            nc.addEventListener(NetStatusEvent.NET_STATUS , netStatus );
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandler);
			nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncHandler);

			creatSharedObject()
		}
		
		/**
		 * 断开连接
		 * 
		 */		
		public function disconnect() :void
		{
			if (nc != null) nc.close()
			
		}
		/**
		 * 呼叫AbcRed5服务器方法 
		 * @param funName AbcRed5服务器端定义的方法字符串形式
		 * @param result  回调函数，响应AbcRed5返回事件
		 * @param value   传递参数，支持Array,String,Number,Object
		 * @return 
		 * 
		 */		
		public function call(funName:String,result:Function,value:*){
		
			nc.call(funName, new Responder(result), value)
				
		}

		/**
		 * 创建共享对象 
		 * @param shareName
		 * 
		 */		
		private function creatSharedObject(shareName:String,shareEvent:Function):void {
			
			var share:SharedObject = SharedObject.getRemote(shareName, nc.uri, false);
			share.connect(nc);
			share.addEventListener(SyncEvent.SYNC, shareEvent);
			share.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			share.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncHandler);

			}
			
					
		/**
		 * 链接状态监听
		 * @param e 
		 * 
		 */		
		private function netStatus(e:NetStatusEvent):void {

            switch (e.info.code) {
				
	           case "NetConnection.Connect.Success" :
				   AbcEventDispatcher.instance.dispatcher(AbcEventType.EVENT001)
		           break;
	            default:
				   AbcEventDispatcher.instance.dispatcher(AbcEventType.ERROR000,"链接AbcRed5服务器失败！")
		           break;
	        }
			
        };
		
		/**
		 * 安全性监听 
		 * @param evt
		 * 
		 */		
		private function securityHandler(evt:SecurityError) :void{

			AbcEventDispatcher.instance.dispatcher(AbcEventType.ERROR000,evt.message)
		}
		
		/**
		 * 异步错误
		 * @param evt
		 * 
		 */		
		private function asyncHandler(evt:AsyncErrorEvent):void {

			AbcEventDispatcher.instance.dispatcher(AbcEventType.ERROR000,evt.message)
		}
		
			
		public function onBWDone():void{}
		
	}
} 

				
